using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;


namespace CKS.EWE.WikiDiscussion
{
    public class Util
    {
        public static Guid GetListGuid(SPWeb Web, string ListName)
        {
            // Return the Guid of a list by its name, or Guid.Empty if no such list
            // Shouldn't really use exeption handling to search for valid list name,
            // but it's a lot easier than a loop to code up quickly.
            try
            {
                return Web.Lists[ListName].ID;
            }
            catch
            {
                return Guid.Empty;
            }
        }


        public static SPListItem GetDiscItem(SPList DiscList, HttpContext context)
        {
            SPListItem DiscItem = null;
            string RootFolder, TopicFilename, testFilename;

            RootFolder = context.Request.QueryString["RootFolder"];
            if (RootFolder != "")
            {
                RootFolder = SPHttpUtility.HtmlDecode(RootFolder);
                TopicFilename = Util.rexUrlFilename.Match(RootFolder).ToString();
                foreach (SPListItem item in DiscList.Folders)
                {
                    testFilename = Util.rexUrlFilename.Match(item.Url).ToString();
                    if (testFilename == TopicFilename)
                    {
                        DiscItem = item;
                        break;
                    }
                }
            }
            return DiscItem;
        }

        
        public static string ReplaceInBodyText(string BodyText, string TopicName)
        {
            if (rexTopicName.Match(BodyText).Success)
            {
                return rexTopicName.Replace(BodyText, TopicName);
            }
            else
            {
                return BodyText;
            }
        }


        public static void RenderMessage(HtmlTextWriter output, string message, string altText)
        {
            output.Write("<table cellpadding='1' cellspacing='0'>");
            output.Write("<tr>");
            output.Write("<td class='ms-toolbar' nowrap>");
            output.Write("<div title='" + altText + "'>");
            output.Write(message);
            output.Write("</div>");
            output.Write("</td></tr>");
            output.Write("</table>");
        }


        public static void LogException(HtmlTextWriter output, Exception ex)
        {
            Util.RenderMessage(output, "*", ex.Message + "\n" + ex.GetType().ToString());
        }


        //Regular expression to isolate the filename from a URL path string
        //[^\/]+$ means match 1 or more of anything that is not a / and is at end of line
        public static System.Text.RegularExpressions.Regex rexUrlFilename =
        new System.Text.RegularExpressions.Regex(
        @"[^\/]+$",
        System.Text.RegularExpressions.RegexOptions.Compiled);

        //Regular expression to isolate a filename ("$1") or its extension ("$2")
        //(.*)\.(.*$) splits a string based on the rightmost dot because it's a greedy algorithm.
        public static System.Text.RegularExpressions.Regex rexFilename =
        new System.Text.RegularExpressions.Regex(
        @"(.*)\.(.*$)",
        System.Text.RegularExpressions.RegexOptions.Compiled);

        //Regular expression to replace <TopicName> token
        public static System.Text.RegularExpressions.Regex rexTopicName =
        new System.Text.RegularExpressions.Regex(
        @"<TopicName>",
        System.Text.RegularExpressions.RegexOptions.Compiled);

        //Regular expression to extract CTID from ows_ContentTypeId='<CTID>'
        public static System.Text.RegularExpressions.Regex rexCTID =
        new System.Text.RegularExpressions.Regex(
        "ContentTypeId=['\"](.*)['\"]",
        System.Text.RegularExpressions.RegexOptions.Compiled);

    }
}
